# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI=3

inherit thexgames

DESCRIPTION="An attempt to make turbomod2 compatible with xtm. Though it should be installed prior to the latter."
HOMEPAGE="http://www.elite-games.ru/x3/scripts/mods.shtml#tt"
SRC_URI="http://thexgames.googlecode.com/files/x3-mod-solid-turbomod2-${PV}-r3.tar.gz"

LICENSE="as-is"
SLOT="0"
KEYWORDS="amd64 x86"
IUSE="extra newfonteng newfontrus nostationdestroy turboplus nomovejump"

RDEPEND="!games-misc/x3-mod-fps-turbomod2"
DEPEND="${RDEPEND}"

src_unpack() {
	unpack ${A}

	if use newfonteng
	then
		use newfontrus && die "Only one of newfonteng or newfontrus could be enabled at the same time"
		mv ENG_new_font/Types types
	elif use newfontrus
	then
		mv RUS_new_font/Types types
	fi

	use nostationdestroy && mv NoStationDestroy/*.pck scripts/
	use turboplus && mv Turbo-plus/scripts/* scripts/
}

src_prepare()
{
	use nomovejump && rm -f "${clns_SOURCESROOT}/scripts/!move.jump.pck"
	thexgames_src_prepare
	
	local nn=$(ls ${clns_SOURCESROOT} -ARr | grep ^[[:digit:]]*\.cat$ | head -n1 | sed 's/\.cat$//')
	nn=$((1+10#${nn}))
	sed -e '3a\MODPAIR='${nn}'\nX3ROOT='${clns_TARGET_ROOT}'\nSOLID_MODS='${clns_R_COLLISIONSROOT_P_MISC}/solid-mods'\n' ${clns_SOURCESROOT}/${_clns_COLLISIONSROOT_P_MISC}/x3-turbomod2.1 >> ${clns_SOURCESROOT}/${_clns_COLLISIONSROOT_P_MISC}/x3-turbomod2 || die "failed to insert vars"
		
	chmod +x ${clns_SOURCESROOT}/${_clns_COLLISIONSROOT_P_MISC}/x3-turbomod2
	chown root:games ${clns_SOURCESROOT}/${_clns_COLLISIONSROOT_P_MISC}/x3-turbomod2
}

src_install()
{
	thexgames_src_install
	
	local nn=$(ls "${clns_SOURCESROOT}" -ARr | grep ^[[:digit:]]*\.cat$ | head -n1 | sed 's/\.cat$//')
	nn=$((1+10#${nn}))
	dosym ${clns_R_COLLISIONSROOT_P_MISC}/solid-mods/X3-Turbo-2_Light.cat /opt/x3/${nn}.cat
	dosym ${clns_R_COLLISIONSROOT_P_MISC}/solid-mods/X3-Turbo-2_Light.dat /opt/x3/${nn}.dat
	dosym ${clns_R_COLLISIONSROOT_P_MISC}/x3-turbomod2 /usr/games/bin/x3-turbomod2 || die
}

pkg_postinst()
{
	thexgames_pkg_postinst
	
	chmod +x /usr/games/bin/x3-turbomod2
	chown root:games /usr/games/bin/x3-turbomod2
	elog "Use /usr/games/bin/x3-turbomod2 to change the mode of the mod"
}
